<script setup>
import axios from 'axios';
import { ref, onMounted, computed } from 'vue';
import { useRouter, useRoute } from 'vue-router'
import { h } from 'vue'
import { ElMessage } from 'element-plus'
import {useTestStore} from '@/stores/test.js'

var passwordPatten = /^\w{6,8}$/

const store = useTestStore()
const router = useRouter()
const password1 = ref("");
const password2 = ref("");
const passwordTip = ref('*请输入正确的密码(6-12位数字,字母组成)')//密码提示

const isPasswordTrue1 = ref(true)
const isPasswordTrue2 = ref(true)

const open = (msg) => {
    ElMessage(msg)
}
const openVn = () => {
    ElMessage({
        message: h('p', null, [
            h('span', null, '消息'),
            h('i', { style: 'color: teal' }, '文字'),
        ]),
    })
}

function isPassword(num) {
    passwordTip.value = '*请输入正确的密码(6-12位数字,字母组成)'
    if (num == 1) {
        isPasswordTrue1.value = passwordPatten.test(password1.value)
    } else if (num == 2) {
        passwordTip.value = '*请输入正确的密码(6-12位数字,字母组成)'
        isPasswordTrue2.value = passwordPatten.test(password2.value)
        if (password1.value != password2.value) {
            passwordTip.value = '两次输入的密码不一致'
        }
    }
}
function submit() {
    if (password2.value == "" || password2.value == "") {
        open("请输入密码")
        isPasswordTrue1.value = false
        isPasswordTrue2.value = false
    } else {
        if (isPasswordTrue1 && isPasswordTrue2) {
            const datas = {
                    "phone":store.changePasswordPhone,
                    "newPassword":password1.value,
                    "againNewPassword":password2.value,
            }
            axios({
                url:'http://192.168.110.221:8080/user/forgotPasswordController',
                method: "post",
                headers: { 'content-type': 'application/x-www-form-urlencoded;charset=utf-8' },
                data: datas
            })
            .then((resp)=>{
                if(resp.data.code==200){
                    open(resp.data.msg)
                    router.push("login")
                }else{
                    open(resp.data.msg)
                }
            })
        } else {
            open('输入有误,请重新输入')
        }
    }
}
</script>

<template>
    <div class="back-box">
        <h1 class="header">设置新密码</h1>
        <div class="body">
            <li>
                <div>
                    <span class="iconfont">&#xe8b2;</span>
                    <input type="password" v-model="password1" placeholder="请输入密码" @blur="isPassword(1)"
                        @keyup.enter="login()">
                </div>
                <p v-if="!isPasswordTrue1">*请输入正确的密码(6-12位数字,字母组成)</p>
            </li>
            <li>
                <div>
                    <span class="iconfont">&#xe8b2;</span>
                    <input type="password" v-model="password2" placeholder="请输入密码" @blur="isPassword(2)"
                        @keyup.enter="login()">
                </div>
                <p v-if="!isPasswordTrue2">{{ passwordTip }}</p>
            </li>
            <li>
                <button class="login-btn" @click="submit()">确认</button>
            </li>
        </div>
    </div>
</template>

<style scoped>
* {
    text-align: center;
    list-style: none;
    text-decoration: none;
}

.back-box {
    position: absolute;
    width: 100%;
    height: 100%;
    background: url("../../assets/img/b5.png");
    background-size: 100% 100%;
}

.header {
    margin-top: 200px;
    font-size: 16px;
}

.body {
    margin-top: 20px;
    overflow: hidden;
}

.body::after {
    display: block;
    content: "";
    clear: both;
}

.iconfont {
    font-family: "iconfont" !important;
    font-size: 20px;
    font-style: normal;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

input {
    text-align: left;
    width: 200px;
    height: 30px;
    outline: none;
    font-size: 16px;
    border: transparent;
    background-color: transparent;
}

.code-text {
    width: 120px;
}

.code-pic {
    display: inline-block;
    height: 32px;
    line-height: 32px;
    margin-left: 8px;
    width: 70px;
    /* border: 1px solid black; */
    vertical-align: top;
    background-color: #fff;
    font-size: 12px;
}

.body>li {
    margin-top: 20px;
}

.body>li::after {
    margin: 0 auto;
    display: block;
    content: "";
    clear: both;
}

.body>li>div~p {
    width: 260px;
    margin: 0 auto;
    color: red;
    text-align: left;
    text-indent: 10px;
}

.body>li>div {
    overflow: hidden;
    display: inline-block;
    border: 1px solid #d6d6d6;
    width: 260px;
    border-radius: 20px;
    background-color: #fff;
}

.code-div {
    width: 180px !important;
    right: 40px;
}

.login-btn {
    width: 260px;
    height: 30px;
    border-radius: 20px;
    border: transparent;
    font-size: 16px;
    font-weight: 600;
    color: #fff;
    background-color: rgba(11, 86, 247, 0.781);
}

li>p {
    font-size: 13px;
}</style>